//PITCH PROGRAM
//CLASS: PLAY
//	-all methods to actually play the game
//BY: ALEX TRACY
import javax.swing.JFrame;
public class Play 
{
	private static int trump = 0;
	private static int leadSuit = -1;
	private static int leadPlayer;
	private static int thrown[] = new int[4];
	private static Deck deck = new Deck();
	private static Player[] players = new Player[4];
	private static Bid bid = new Bid(0,players);
	private static int currentBid;
	private static int playersTurn;
	private static Team teams[] = new Team[2];
	private static boolean gameOver = false;
	private static int bidder = 1;
	private static int boss = -1;
	private static boolean twoOut = false;
	private static boolean jackOut = false;
	
	//initialize variables, while the game is not over
	//shuffle and deal cards get bids and hand
	public static void play()
	{
		
		players[0] = new Human_Player(0);
		players[1] = new AI_Player(1);
		players[2] = new AI_Player(2);
		players[3] = new AI_Player(3);
		teams[0] = new Team(0);
		teams[1] = new Team(1);
		for (int i = 0 ; i < thrown.length ; i++)
			thrown[i] = -1;
		GUI g = new GUI(players);
		g.setSize(720,720);
		g.setVisible(true);
		g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Card.initImgs();
		
		while ( gameOver != true)
		{
			twoOut = false;
			jackOut = false;
			shuffleDeal();
			g.repaint();
			getBids();
			g.setBidLabel(getCurrentBid());
			g.setTrumpLabel(trump);
			int count = 0;
		
			//for 0 - 6 each player plays a card starting
			//with the lead player, then endOfHand functions are called
			//after 6 end of Round funtions are called, check if game is over
			//after every round
			for (int j = 0 ; j < 6 ; j++)
			{
				count = 0;	
				for (int i = 0 ; i < 4 ; i++)
					thrown[i] = -1;
				playersTurn = leadPlayer;
				leadSuit = -1;
				while (count < players.length)
				{
					thrown[playersTurn] = 
						players[playersTurn].play(
							thrown, leadSuit, trump,count,boss,jackOut,twoOut,
							j);
					if (count == 0 )
						leadSuit = Card.getSuit(thrown[playersTurn]);
					players[playersTurn].sortHand();
					g.repaint();
					playersTurn = getNextPlayer(playersTurn);
					count++;
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				endOfHand();
			}
			endOfRound();
			g.repaint();
			for (int i = 0 ; i < 2 ; i++)
				if ( teams[i].getCurrentScore() >= 21)
					gameOver = true;
		}
	}//End play
	
	//shuffle and deal cards, then sort hands
	public static void shuffleDeal()
	{
		
		deck.shuffle();
		deck.shuffle();
		deck.deal(players);
		for (int i = 0; i < players.length;i++)
		{
			players[i].sortHand();
		}
		
	}//End shuffleDeal
	
	//get bids and determine current high bid
	//and player that bid
	//also set team bids
	public static void getBids()
	{
		for (int i = 0 ; i < 2 ; i++)
			teams[i].setBid(0);
		bidder = bid.getBids();
		currentBid = players[bidder].getBid();
		leadPlayer = bidder;
		trump = players[leadPlayer].getBidS();
		boss = (trump + 1) * 12 + trump;
		teams[players[leadPlayer].getTeam()].setBid(
				players[leadPlayer].getBid());
		
	}//End getBids
	
	//return trump
	public static int getTrump()
	{
		return trump;
	}//End getTrump
	
	//return lead suit
	public int getLeadS()
	{
		return leadSuit;
	}//End getLeadS
	
	//return players array
	public Player[] getPlayers()
	{
		return players;
	}//End getPlayers
	
	//return thrown array
	public static int[] getThrown()
	{
		return thrown;
	}//End getThrown
	
	//get specified thrown card
	public static int getThrown(int i)
	{
		return thrown[i];
	}//End getThrown(int)
	
	//set trump
	public static void setTrump(int i)
	{
		trump = i;
	}//End setTrump
	
	//set lead suit
	public void setLeadS(int i)
	{
		leadSuit = i;
	}//End setLeadS
	
	//set thrown card
	public static void setThrown(int i, int c)
	{
		thrown[i] = c;
	}//End setThrown
	
	//return next player
	public static int getNextPlayer(int p)
	{
		if (p == 3)
			return 0;
		else
			return ++p;
	}//End getNextPlayer
	
	//return whos turn it is to go
	public static int whosTurn()
	{
		return playersTurn;
	}//End whosTurn
	
	//return player to lead hand
	public static int getLeadPlayer()
	{
		return leadPlayer;
	}//End getLeadPlayer
	
	//return a string version of the current bid and bidder
	public static String getCurrentBid()
	{
		return ("Bidder: " + bidder + " Bid: " + currentBid + " ");
	}//End getCurrentBid
	
	//set the player to lead the next hand
	public static void setLeadPlayer(int i)
	{
		leadPlayer = i;
	}//End setLeadPlayer
	
	//function to be done at end of hand
	//determine who won the hand, give the
	//cards to the winning team, and check
	//if the two or jack has been out or
	//the boss has been played
	private static void endOfHand()
	{
		int handWinner = determineHandWinner();
		leadPlayer = handWinner;
		giveCardsToWinningTeam(handWinner);
		checkForJack();
		checkForTwo();
		checkForBoss();
	}//End endOfHand
	
	//functions to be done at end of round
	//determine who gets the score points
	//then either add or subtract the points from the 
	//teams score and update the score panel
	private static void endOfRound()
	{
		if (teams[0].getHigh(trump) > teams[1].getHigh(trump))
			teams[0].addToScore();
		else
			teams[1].addToScore();
		
		if (teams[0].getLow(trump) < teams[1].getLow(trump))
			teams[0].addToScore();
		else
			teams[1].addToScore();
		
		if (teams[0].getGame() > teams[1].getGame())
			teams[0].addToScore();
		else if (teams[0].getGame() < teams[1].getGame())
			teams[1].addToScore();
		
		for (int i = 0 ; i < 2 ; i++)
			teams[i].calcScore(trump);
		System.out.println();
		ScorePanel.updateScores(teams[0].getScores(),teams[1].getScores());
		for (int i = 0 ; i < 2 ; i++)
			teams[i].clearTaken();
	}//End endOfRound
	
	//check if two has been out and update
	//twoOut variable
	private static void checkForTwo()
	{
		for (int i = 0 ; i < thrown.length ; i ++)
			if (Card.getSuit(thrown[i]) == trump && Card.isDeuce(thrown[i]))
			{
				twoOut = true;
				break;
			}
	}//End checkForTwo
	
	//check if jack has been out and update
	//jackOut variable
	private static void checkForJack()
	{
		for (int i = 0 ; i < thrown.length ; i ++)
			if (Card.getSuit(thrown[i]) == trump && Card.isJack(thrown[i]))
			{
				jackOut = true;
				break;
			}
	}//End checkForJack
	
	//check if the boss has been out if it has
	//decrement the boss variable and call
	//check for boss again
	private static void checkForBoss()
	{
		for (int i = 0 ; i < thrown.length ; i++)
		{
			if (thrown[i] == boss)
			{
				--boss;
				checkForBoss();
			}
		}	
	}//end checkForBoss
	
	//determine who has won the hand
	private static int determineHandWinner()
	{
		int bestCard = -1;
		int handWinner = -1;
		for (int i = 0 ; i < thrown.length ; i++)
			if (Card.getSuit(thrown[i]) == trump && 
					thrown[i] > bestCard)
			{
				bestCard = thrown[i];
				handWinner = i;
			}
		if ( bestCard == -1)
			for (int i = 0 ; i < thrown.length ; i++)
				if (Card.getSuit(thrown[i]) == leadSuit && 
						thrown[i] > bestCard)
				{
					bestCard = thrown[i];
					handWinner = i;
				}
		//	System.out.println(leadSuit + " " + trump + " " + Card.getCard(bestCard) + " " + handWinner);
			return handWinner;
	}//end determineHandWinner
	
	//give cards to the team that has won the hand
	private static void giveCardsToWinningTeam(int hW)
	{
		teams[players[hW].getTeam()].addTaken(thrown);
	}//end giveCardsToWinnningTeam
	
}//End Play
